package com.t.bricks.bussiness.service.orderscomment.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.db.entity.orderscomment.OrdersComment;
import com.t.bricks.bussiness.service.common.CommonService;
import com.t.bricks.bussiness.service.orderscomment.OrdersCommentService;
import com.t.bricks.model.FileInfo;
import com.t.bricks.model.MsgEmity;
import com.t.bricks.utils.spring.ModuleUtil;

/**
 * 订单评论'OrdersComment表'基本业务操作类接口实现
 * @author HuangXinBian
 * @DateTime 2022年06月22日 15:25:30
 */
@Service
public class OrdersCommentServiceImpl implements OrdersCommentService<OrdersComment> {
	
	/**
	 * 订单评论时间限制(默认15天内)
	 */
	@Value("${app.order.comment.space:15}")
	private Integer iSpace;

	/**
	 * 文件上传及保存的根路径
	 */
	@Autowired
	private CommonService commonService;
	
	/**
	 * 自定义新增
	 * @param ordersComment 订单评论 对象实体
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity addCustom(OrdersComment ordersComment) {
		MsgEmity me = ModuleUtil.objToEntity(ordersComment, OrdersComment.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		ordersComment = me.getData();
		ordersComment.setsAuditId(null);
		ordersComment.setsAudit(null);
		ordersComment.setdAuditDate(null);
		ordersComment.setsCustomerAliases("随便");
		ordersComment.setsCustomerPhoto(null);
		
		me = ModuleUtil.validDbEntityByAdd(ordersComment);// 通用的基本数据验证,如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		me = this.callMapping("checkOrders", ordersComment.getsOrderId(),
				ordersComment.getsWaresId(), iSpace, ModuleUtil.currentLoginUserId());
		if (!me.isSuccess()) {
			return MsgEmity.err(8020, "执行'验证订单操作权限'失败");
		}
		
		Integer iState = me.getData();//1111,分别代表:当前用户订单状态6/是否当前用户订单/期限内订单/已经评论
		if (iState.intValue() < 1000) {//订单状态不是6或者不属于当前用户,0或10或10或0011
			return MsgEmity.err(8021, "订单确认后才能进行评价");
		}
				
		if (iState.intValue() < 1100) {//0或10或11
			return MsgEmity.err(8021, "只有该订单对应的客户才能进行评价");
		}

		if (iState.intValue() < 1110) {//100或101
			return MsgEmity.err(8022, "该订单已经过了评论期");
		}

		if (iState.intValue() != 1110) {
			return MsgEmity.err(8023, "订单下该商品已存在评论记录");
		}
		
		ordersComment.setuId(null);//清空记录编号
		
		me = ModuleUtil.supplyDbEntityAttrByAdd(ordersComment);// 补充数据,如添加id、当前用户、当前时间等
		if (!me.isSuccess()) {
			return me;
		}

		String local = String.format("/%s/%s/", ordersComment.getsCreator(), ordersComment.getsOrderId());
		me = commonService.fileTree(local, "OrdersComment");
		if (!me.isSuccess()) {
			return me;
		}
		
		FileInfo fileInfo = me.getData();
		if (null == fileInfo.getClient() || fileInfo.getClient().length < 1) {//没有图片
			return this.add(ordersComment);//交回通用方法
		}
		
		StringBuilder sb = new StringBuilder(200);
		for (FileInfo info : fileInfo.getClient()) {
			sb.append(info.getsPath().replaceAll("/OrdersComment/", "/")).append(";");
		}
		sb.setLength(sb.length() -1);
		
		ordersComment.setsCommentImgs(sb.toString());
		
		return this.add(ordersComment);//交回通用方法
	}

	/**
	 * 自定义修改
	 * @param ordersComment 订单评论 对象实体
	 * @return MsgEmity
	 */
	@Override
	public MsgEmity editCustom(OrdersComment ordersComment) {
		MsgEmity me = ModuleUtil.objToEntity(ordersComment, OrdersComment.class);//转换数据,因为如果传递的是String,则可能是进行了跨包访问,要先用Json转换成实体对象
		if (!me.isSuccess()) {
			return me;
		}
		
		OrdersComment ordersCommentData = me.getData();
		
		me = ModuleUtil.validBaseDataByEdit(ordersCommentData);// 通用的基本数据验证(编辑),如果要进行比较复杂的数据验证则可以屏蔽掉此方法自行编写
		if (!me.isSuccess()) {
			return me;
		}
		
		String uId = ordersCommentData.getuId();
		if ("".equals(uId)) {
			return MsgEmity.err(8020, "记录编号不能为空");
		}
		
		me = this.getValueByFieldName(uId, "sOrderId,sWaresId,sCreator", false);
		if (!me.isSuccess()) {
			return MsgEmity.err(8021, "查找相关记录失败");
		}
		
		String userId = ModuleUtil.currentLoginUserId();
		OrdersComment temp = me.getData();
		ordersCommentData.setsOrderId(temp.getsOrderId());
		ordersCommentData.setsWaresId(temp.getsWaresId());
		ordersCommentData.setsCreator(temp.getsCreator());
		
		if (!userId.equals(temp.getsCreator())) {
			return MsgEmity.err(8022, "当前操作用户与记录所有者不符");
		}
		
		me = this.callMapping("checkOrders", ordersCommentData.getsOrderId(),
				ordersCommentData.getsWaresId(), iSpace, ModuleUtil.currentLoginUserId());
		if (!me.isSuccess()) {
			return MsgEmity.err(8023, "执行'验证订单操作权限'失败");
		}
		
		Integer iState = me.getData();//1111,分别代表:当前用户订单状态6/是否当前用户订单/期限内订单/已经评论
		if (iState.intValue() < 1000) {//订单状态不是6或者不属于当前用户,0或10或10或0011
			return MsgEmity.err(8024, "订单确认后才能进行评价");
		}
		
		if (iState.intValue() < 1100) {//0或10或11
			return MsgEmity.err(8025, "只有该订单对应的客户才能进行评价");
		}

		if (iState.intValue() < 1110) {//1100或1101
			return MsgEmity.err(8026, "该订单已经过了评论期");
		}

		if (iState.intValue() != 1111) {//
			return MsgEmity.err(8027, "订单下该商品并不存在评论记录");
		}
		
		ModuleUtil.supplyDbEntityAttrByEdit(ordersCommentData);// 补充数据

		String local = String.format("/%s/%s/", ordersCommentData.getsCreator(), ordersCommentData.getsOrderId());
		me = commonService.fileTree(local, "OrdersComment");
		if (!me.isSuccess()) {
			return me;
		}
		
		FileInfo fileInfo = me.getData();
		if (null == fileInfo.getClient() || fileInfo.getClient().length < 1) {//没有图片
			return this.edit(ordersCommentData);//交回通用方法,注意:更新成功后对象属性iVersion值将被+1
		}
		
		StringBuilder sb = new StringBuilder(200);
		for (FileInfo info : fileInfo.getClient()) {
			sb.append(info.getsPath().replaceAll("/OrdersComment/", "/")).append(";");
		}
		sb.setLength(sb.length() -1);
		
		ordersCommentData.setsCommentImgs(sb.toString());
		
		//--提交数据保存--//
		return this.edit(ordersCommentData);//交回通用方法,注意:更新成功后对象属性iVersion值将被+1
	}
	
	/**
	 * 查询订单下各个商品的评价检验值
	 * @param sOrderId 订单编号
	 * @return
	 */
	@Override
	public MsgEmity findOrderCheck(String sOrderId) {
		if (null == sOrderId || "".equals(sOrderId.trim())) {
			return MsgEmity.err(8001, "订单编号为空");
		}
		
		String sCreator = ModuleUtil.currentLoginUserId();
		return this.callMapping("findOrderCheck", sOrderId, sCreator, iSpace);
	}

}
